﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace McMySkin_Installer
{
    public partial class RestoreForm : NewForm
    {
        public RestoreForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "请选择您的 .minecraft\\bin 文件夹";
            folderBrowserDialog1.ShowDialog();

            Config.backupDir = folderBrowserDialog1.SelectedPath;
            label4.Text = "还原目录:" + Config.backupDir;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked && string.IsNullOrEmpty(Config.backupDir))
            {
                MessageBox.Show("文件没有选定 =w=");
                return;
            }
            else if (radioButton2.Checked || radioButton3.Checked)
            {
                if (!File.Exists(Config.backupDir + "\\minecraft.jar"))
                {
                    MessageBox.Show("不存在的文件!");
                    return;
                }
            }

            this.restore();
        }

        private void restore()
        {

            if (!string.IsNullOrEmpty(Config.backupDir))
            {
                Installer installer = null;

                if (Config.createBackup && radioButton1.Checked)
                {
                    if (File.Exists(Config.backupDir + "\\minecraft.jar.mmsbak"))
                    {
                        File.Delete(Config.backupDir + "\\minecraft.jar");
                        File.Copy(Config.backupDir + "\\minecraft.jar.mmsbak", Config.backupDir + "\\minecraft.jar");
                        File.Delete(Config.backupDir + "\\minecraft.jar.mmsbak");
                        MessageBox.Show("还原完成", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("没有发现备份");
                    }
                }
                else if (radioButton2.Checked)
                {
                    installer = new Installer(Config.backupDir + "\\minecraft.jar", InstallTpye.INSTALL_MOJANG);
                }
                else if (radioButton3.Checked)
                {
                    installer = new Installer(Config.backupDir + "\\minecraft.jar", InstallTpye.INSTALL_SM);
                }
                else
                {
                    MessageBox.Show("未知的错误哦~ =w=");
                }

                new Thread(() => { installer.Run(); }).Start();
                new Thread(() =>
                {
                    button1.Enabled = false;
                    while (!installer.done)
                    {
                        label2.Text = " " + progressBar1.Value + "%";
                        progressBar1.Value = (int)(installer.Progress * 100);
                    } MessageBox.Show("还原完成", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    label2.Text = "未开始还原";
                    button1.Enabled = true; progressBar1.Value = 0;
                }).Start();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = global::McMySkin_Installer.Properties.Resources.closeOnMouse;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = global::McMySkin_Installer.Properties.Resources.close;
        }


    }
}
